package com.sudo.api.converter;

import com.sudo.common.exception.BizException;
import org.springframework.core.convert.converter.Converter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * localDate入参格式化
 *
 * @author DayDayUp
 * @datetime 2023-08-31 10:18
 **/
public class LocalDateTimeConverter implements Converter<String, LocalDateTime> {

    private final DateTimeFormatter dateTimeFormatter;

    public LocalDateTimeConverter(DateTimeFormatter dateTimeFormatter) {
        this.dateTimeFormatter = dateTimeFormatter;
    }

    @Override
    public LocalDateTime convert(String source) {
        try {
            return LocalDateTime.parse(source, dateTimeFormatter);
        }catch (Exception e){
            throw new BizException("时间格式不正确");
        }
    }
}
